
/**
 * @class Kohive.HttpProvider
 * @extends Ext.ux.HttpProvider
 * Extends the HttpProvider so we can use it with our PHP backend.
 */
Kohive.HttpProvider = function(config) {
	var config = config || {};

	Ext.applyIf(config, {
				url			: '/states',
				autoRead	: false,
				delay		: 750,
				queue		: {},
				sendMethod	: 'POST',
				readMethod	: 'GET'
			});

	Kohive.HttpProvider.superclass.constructor.call(this, config);
};
Ext.extend(Kohive.HttpProvider, Ext.ux.HttpProvider, {

			/**
			 * Initalizes the Kohive HTTTP state provider by sending off a request to the
			 * server and setting this.state
			 */
			initState		: function() {
				this.state = {};

				// Success function
				var successFn = function(response) {
					var o = {};

					// Try to decode the response text and log out the error if set
					try {
						o = Ext.decode(response.responseText);
					} catch (e) {
						if (true === this.logFailure) {
							this.log(this.readFailureText, e, response);
						};
						return;
					};

					// Loop through each of the items in response
					for (var i = 0; i < o.length; i++) {
						var n = o[i].key;
						var v = o[i].data;

						// Decode the value and add it into the state config
						this.state[n] = this.decodeValue(v);
					};
				};

				// Failure function
				var failureFn = function(response) {
					if (true === this.logFailure) {
						this.log(this.readFailureText, e, response);
					};
				};

				var o = {
					url		: this.readUrl || this.url,
					method	: this.getMethod || 'GET',
					scope	: this,
					success	: successFn,
					failure	: failureFn
				};

				// Send off the request
				Ext.Ajax.request(o);
			},

			// private
			queueChange		: function(name, value) {
				var changed = undefined === this.state[name]
						|| this.state[name] !== value;
				var o = {};
				var i;
				var found = false;
				if (changed) {
					var n = name;
					var v = this.encodeValue(value);

					if (this.queue[n]) {
						this.queue[n] = v;
						found = true;
					}
					if (false === found) {
						this.queue[name] = this.encodeValue(value);
					}
					this.dirty = true;
				}
				return changed;
			},

			// private
			submitState		: function() {
				if (!this.dirty) {
					this.dt.delay(this.delay);
					return;
				}
				this.dt.cancel();

				var o = {
					url		: this.saveUrl || this.url,
					method	: this.sendMethod || 'POST',
					scope	: this,
					success	: this.onSaveSuccess,
					failure	: this.onSaveFailure,
					queue	: Ext.ux.clone(this.queue),
					params	: {}
				};

				for (r in o.queue) {
					Ext.apply(o.params, {
								key		: r,
								data	: o.queue[r]
							});
				};

				// be optimistic
				this.dirty = false;

				Ext.Ajax.request(o);
			},

			onSaveSuccess	: function(response, options) {
				if (this.started) {
					this.start();
				};

				this.fireEvent('savesuccess', this);
			},

			// private
			readState		: function() {

				var o = {
					url		: this.readUrl || this.url,
					method	: this.getMethod || 'GET',
					scope	: this,
					success	: this.onReadSuccess,
					failure	: this.onReadFailure
				};

				Ext.Ajax.request(o);
			},

			onReadSuccess	: function(response, options) {
				var o = {};
				try {
					o = Ext.decode(response.responseText);
				} catch (e) {
					if (true === this.logFailure) {
						this.log(this.readFailureText, e, response);
					};
					return;
				};

				for (item in o) {
					var n = item;
					var v = o[item];

					this.state[n] = v;
				};

				// this.queue = [];
				this.dirty = false;

				if (true === this.logSuccess) {
					this.log(this.readSuccessText, data, response);
				};

				this.fireEvent('readsuccess', this);
			}

		});